package com.ruoyi.generator.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.generator.domain.FromBuildPage;
import com.ruoyi.generator.service.IFromBuildPageService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.generator.domain.AppBuildInfo;
import com.ruoyi.generator.service.IAppBuildInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 移动端设计应用信息Controller
 *
 * @author ZGL
 * @date 2025-05-09
 */
@RestController
@RequestMapping("/generator/AppBuildInfo")
public class AppBuildInfoController extends BaseController
{
    @Autowired
    private IAppBuildInfoService appBuildInfoService;

    @Autowired
    private IFromBuildPageService fromBuildPageService;

    /**
     * 查询移动端设计应用信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AppBuildInfo appBuildInfo)
    {
//        startPage();
        List<AppBuildInfo> list = appBuildInfoService.selectAppBuildInfoList(appBuildInfo);
        return getDataTable(list);
    }

    /**
     * 导出移动端设计应用信息列表
     */
    @Log(title = "移动端设计应用信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppBuildInfo appBuildInfo)
    {
        List<AppBuildInfo> list = appBuildInfoService.selectAppBuildInfoList(appBuildInfo);
        ExcelUtil<AppBuildInfo> util = new ExcelUtil<AppBuildInfo>(AppBuildInfo.class);
        util.exportExcel(response, list, "移动端设计应用信息数据");
    }

    /**
     * 获取移动端设计应用信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(appBuildInfoService.selectAppBuildInfoById(id));
    }

    /**
     * 新增移动端设计应用信息
     */
    @Log(title = "移动端设计应用信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody AppBuildInfo appBuildInfo)
    {
        appBuildInfo = appBuildInfoService.insertAppBuildInfo(appBuildInfo);
        return AjaxResult.success(appBuildInfo);
    }

    /**
     * 修改移动端设计应用信息
     */
    @Log(title = "移动端设计应用信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody AppBuildInfo appBuildInfo)
    {
        AppBuildInfo appInfo = new AppBuildInfo();
        appInfo.setId(appBuildInfo.getId());
        appInfo.setAppName(appBuildInfo.getAppName());
        appInfo.setAppDes(appBuildInfo.getAppDes());
        return toAjax(appBuildInfoService.updateAppBuildInfo(appInfo));
    }

    /**
     * 删除移动端设计应用信息
     */
    @Log(title = "移动端设计应用信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(appBuildInfoService.deleteAppBuildInfoByIds(ids));
    }

    /**
     * 上传app图标
     */
    @Log(title = "上传app图标", businessType = BusinessType.UPDATE)
    @PostMapping("/uploadAppIcon")
    public AjaxResult avatar(@RequestParam("appIcon") MultipartFile file, @RequestParam("id") Long id) throws Exception
    {
        if (!file.isEmpty()){
            String appIconUrl = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            AppBuildInfo appBuild = new AppBuildInfo();
            appBuild.setAppIcon(appIconUrl);
            appBuild.setId(id);
            appBuildInfoService.updateAppBuildInfo(appBuild);

            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", appIconUrl);
            return ajax;
        }
        return error("上传失败");
    }

    /**
     * 获取平台所有的appKey
     */
    @GetMapping("/getAllAppKey")
    public AjaxResult getAllAppKey()
    {
        AppBuildInfo appBuildInfo = new AppBuildInfo();
        appBuildInfo.setStatus("0");
        List<AppBuildInfo> appBuildInfos = appBuildInfoService.selectAppBuildInfoList(appBuildInfo);
        ArrayList<String> retList = new ArrayList<>();
        appBuildInfos.forEach(item -> {
            retList.add(item.getAppKey());
        });
        return success(retList);
    }

    /**
     * 根据appKey获取移动端信息
     */
    @GetMapping("/getAppInfoByAppKey")
    public AjaxResult getAppInfoByAppKey(@RequestParam String appKey){
        AppBuildInfo appBuildInfo = new AppBuildInfo();
        appBuildInfo.setAppKey(appKey);
        appBuildInfo.setStatus("0");
        List<AppBuildInfo> appBuildInfos = appBuildInfoService.selectAppBuildInfoList(appBuildInfo);
        if(appBuildInfos.size() <= 0){
            return error("未找到应用信息");
        }
        FromBuildPage fromBuildPage = new FromBuildPage();
        fromBuildPage.setAppKey(appKey);
        fromBuildPage.setStatus("0");
        fromBuildPage.setParentId("0");
        // 获取首页的buildKey
        List<FromBuildPage> fromBuildPages = fromBuildPageService.selectFromBuildPageList(fromBuildPage);
        if(fromBuildPages == null || fromBuildPages.size() <= 0){
            return error("未找到首页1");
        }
        fromBuildPage.setParentId(fromBuildPages.get(0).getId().toString());
        fromBuildPages = fromBuildPageService.selectFromBuildPageList(fromBuildPage);
        if(fromBuildPages == null || fromBuildPages.size() <= 0){
            return error("未找到首页2");
        }
        HashMap<String,Object> retMap = new HashMap<>();
        retMap.put("appInfo", appBuildInfos.get(0));
        retMap.put("homePageKey", fromBuildPages.get(0).getBuildKey());
        return AjaxResult.success("成功", retMap);

    }
}
